package cn.edu.sdu.sol.intake_system.service;

import cn.edu.sdu.sol.intake_system.model.po.Department;
import cn.edu.sdu.sol.intake_system.model.po.Score;
import cn.edu.sdu.sol.intake_system.model.po.SysUser;
import cn.edu.sdu.sol.intake_system.model.po.Will;
import cn.edu.sdu.sol.intake_system.model.vo.DataRequest;
import cn.edu.sdu.sol.intake_system.model.vo.DataResponse;
import cn.edu.sdu.sol.intake_system.repository.DepartmentRepository;
import cn.edu.sdu.sol.intake_system.repository.ScoreRepository;
import cn.edu.sdu.sol.intake_system.repository.UserRepository;
import cn.edu.sdu.sol.intake_system.repository.WillRepository;
import cn.edu.sdu.sol.intake_system.utils.DateUtil;
import cn.edu.sdu.sol.intake_system.utils.JwtUtil;
import cn.edu.sdu.sol.intake_system.utils.PersonInfUtils;
import cn.edu.sdu.sol.intake_system.utils.VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ScoreService {
    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    UserRepository userRepository;

    VO vo = new VO();

    @Autowired
    PersonInfUtils personInfUtils;
    @Autowired
    private WillRepository willRepository;
    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private ScoreRepository scoreRepository;


    public DataResponse passPen(DataRequest req, String token) {
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        Integer personId = req.getInteger("personId");
        Boolean penPassed = req.getBoolean("penPassed");
        Integer departmentId = req.getInteger("departmentId");
        Integer selfDepartmentId = sysUser.get().getAdminInf().getDepartment().getDepartmentId();
        if(departmentId != selfDepartmentId){
            return vo.messageError("不能操作非本部门人员录取信息！");
        }
        Will will = willRepository.findWillByPersonInfPkAAndDepartmentId(personId, departmentId).get();
        Score score = will.getScore();
        score.setPenPassed(penPassed);
        scoreRepository.saveAndFlush(score);
        return vo.ok();
    }

    public DataResponse passFace(DataRequest req, String token) {
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        Integer personId = req.getInteger("personId");
        Boolean facePassed = req.getBoolean("facePassed");
        Integer departmentId = req.getInteger("departmentId");
        Integer selfDepartmentId = sysUser.get().getAdminInf().getDepartment().getDepartmentId();
        if(departmentId != selfDepartmentId){
            return vo.messageError("不能操作非本部门人员录取信息！");
        }
        Will will = willRepository.findWillByPersonInfPkAAndDepartmentId(personId, departmentId).get();
        Score score = will.getScore();
        score.setFacePassed(facePassed);
        scoreRepository.saveAndFlush(score);
        return vo.ok();
    }

    public DataResponse pass(DataRequest req, String token) {
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        Integer personId = req.getInteger("personId");
        Boolean isPassed = req.getBoolean("isPassed");
        Integer departmentId = req.getInteger("departmentId");
        Integer selfDepartmentId = sysUser.get().getAdminInf().getDepartment().getDepartmentId();
        if(departmentId != selfDepartmentId){
            return vo.messageError("不能操作非本部门人员录取信息！");
        }
        Will will = willRepository.findWillByPersonInfPkAAndDepartmentId(personId, departmentId).get();
        Score score = will.getScore();
        score.setIsPassed(isPassed);
        score.setPassTime(DateUtil.getTime());
        scoreRepository.saveAndFlush(score);
        return vo.ok();
    }

    public DataResponse saveInterviewRecord(DataRequest req, String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        Integer personId = req.getInteger("personId");
        Integer departmentId = sysUser.get().getAdminInf().getDepartment().getDepartmentId();
        Department department = departmentRepository.findDepartmentByDepartmentId(departmentId).get();
        Will will = willRepository.findWillByPersonInfPkAAndDepartmentId(personId, departmentId).get();
        Score score = will.getScore();
        String interviewRecord = req.getString("interviewRecord");
        score.setInterviewRecord(interviewRecord);
        scoreRepository.saveAndFlush(score);
        return vo.ok();
    }
}
